package com.chris.libs.base.x.mvpp.recycler;

import android.content.Context;
import android.view.View;

import com.chris.libs.base.x.mvpp.callbacks.BaseRecyclerView;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Create by Chris Chan
 * Create on 2023/4/1 8:12
 * Use for:
 * Explain:
 */
public class DefaultRvAdapter<D, VH extends BaseRecyclerHolder<D>> extends BaseRecyclerAdapter<D, VH> {
    private int layoutId;
    private Class<VH> viewHolderClass;

    public static <D, VH extends BaseRecyclerHolder<D>> DefaultRvAdapterBuilder<D, VH> builder() {
        return DefaultRvAdapterBuilder.newBuilder();
    }

    public DefaultRvAdapter(String tag, Context context, List<D> dataList, BaseRecyclerView.OnItemClickListener<D> onItemClickListener) {
        super(tag, context, dataList, onItemClickListener);
    }

    public static <D, VH extends BaseRecyclerHolder<D>> DefaultRvAdapter<D, VH> create(String tag, Context context, int layoutId, Class<VH> viewHolderClass, List<D> dataList, BaseRecyclerView.OnItemClickListener<D> onItemClickListener) {
        return new DefaultRvAdapter<>(tag, context, layoutId, viewHolderClass, dataList, onItemClickListener);
    }

    public DefaultRvAdapter(String tag, Context context, int layoutId, Class<VH> viewHolderClass, List<D> dataList, BaseRecyclerView.OnItemClickListener<D> onItemClickListener) {
        super(tag, context, dataList, onItemClickListener);
        this.layoutId = layoutId;
        this.viewHolderClass = viewHolderClass;
    }

    @Override
    protected int layoutId(int viewType) {
        return layoutId;
    }

    @Override
    protected VH createViewHolder(View itemView) {
        Constructor<VH> constructor = null;

        try {
            //找到viewHolderClass的构建器 4个参数
            constructor = viewHolderClass.getDeclaredConstructor(String.class, Context.class, View.class, BaseRecyclerView.OnItemClickListener.class);
            //执行构建器，创建对象
            return constructor.newInstance(tag, context, itemView, onItemClickListener);
        } catch (NoSuchMethodException e1) {
            try {
                //找到viewHolderClass的构建器 3个参数
                constructor = viewHolderClass.getDeclaredConstructor(Context.class, View.class, BaseRecyclerView.OnItemClickListener.class);
                //执行构建器，创建对象
                return constructor.newInstance(context, itemView, onItemClickListener);
            } catch (NoSuchMethodException e2) {
                //如果有监听器的构造器没有找到，那就找没有监听器的
                try {
                    constructor = viewHolderClass.getDeclaredConstructor(Context.class, View.class);
                    //执行构建器，创建对象
                    return constructor.newInstance(context, itemView);
                } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) {
                    //ex.printStackTrace();
                    throw new RuntimeException("没找到符合要求的构造器");
                }

            } catch (IllegalAccessException | InstantiationException | InvocationTargetException e3) {
                e3.printStackTrace();
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e4) {
            e4.printStackTrace();
        }

        return null;
    }

    @Override
    protected void refreshItemView(VH holder, D item) {
        holder.refreshView(item);
    }
}
